package com.example.tms.mapper;

import com.example.tms.bean.WaybillMgmt;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface WaybillMapper extends JpaRepository<WaybillMgmt,String>, JpaSpecificationExecutor<WaybillMgmt> {

    //查询运单信息（运单id）
    @Query(value = "select * from waybill_mgmt where ydid = ?1 and sfcz = true", nativeQuery = true)
    WaybillMgmt findByYdid(String ydid);

    //查询运单信息（运单号）
    @Query(value = "select * from waybill_mgmt where ydh = ?1 and sfcz = true", nativeQuery = true)
    WaybillMgmt findByYdh(String ydh);

    //运单发车
    @Query(value = "update waybill_mgmt set ydj =?3, ydzt = '已发车', fcrq = ?2 where ydid = ?1", nativeQuery = true)
    @Modifying
    @Transactional
    void startWaybill(String ydid, String fcrq, String ydj);

    //删除运单
    @Query(value = "update waybill_mgmt set sfcz = false where ydid = ?1", nativeQuery = true)
    @Modifying
    @Transactional
    void deleteWaybill(String ydid);

    //修改运单状态（运单号）
    @Query(value = "update waybill_mgmt set ydzt = ?2 where ydh = ?1", nativeQuery = true)
    @Modifying
    @Transactional
    void modifyWaybill(String ydh, String ydzt);

    //查询当天运单数（运单日期）
    Long countByYdrq(String ydrq);

}
